package com.sohoometer.web.taglib;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;

public class MathTaglib extends ComponentTagSupport {
	private String ognlExpression;

	@Override
	public Component getBean(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res) {
		return new MathBean(stack) {
		};
	}

	@Override
	protected void populateParams() {
		super.populateParams();
		MathBean mathBean = (MathBean) component;
		mathBean.setOgnlExpression(ognlExpression);
	}

	public String getOgnlExpression() {
		return ognlExpression;
	}

	public void setOgnlExpression(String ognlExpression) {
		this.ognlExpression = ognlExpression;
	}

	private class MathBean extends Component {
		private String ognlExpression;

		public MathBean(ValueStack stack) {
			super(stack);
		}

		public String getOgnlExpression() {
			return ognlExpression;
		}

		public void setOgnlExpression(String ognlExpression) {
			this.ognlExpression = ognlExpression;
		}

		@Override
		public boolean start(Writer writer) {
			Object findValue = stack.findValue(ognlExpression);
			if(findValue==null) { return false;}
			try {
				int num = (Integer) findValue;
				writer.write(Math.pow(10,- num) + "");
			} catch (Exception e) {
				System.out.println("输入的数字有误请检查");
				return false;
			}
			return true;
		}

	}

}
